﻿using System;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Configuration;
using AukcjeInternetowe.Models;
using System.Collections.Generic;
using System.Data.SqlClient;

namespace AukcjeInternetowe.Providers
{
    public class OracleDataProvider : IDataProvider
    {
        public static AuctionRepository dbc = null;

        public OracleDataProvider()
        {
            if(dbc == null)
                dbc = new AuctionRepository();
        }

        public List<Category> GetCategories()
        {
            List<Category> categories = new List<Category>();

            foreach (var i in dbc.categories.AsEnumerable().ToList())
            {
                categories.Add(new Category()
                {
                    CategoryId = Convert.ToInt32(i.CategoryId),
                    CategoryName = i.CategoryName,
                    CategoryDescription = i.CategoryDescription
                });
            }

            return categories;
        }

        public List<ItemCategoryModel> GetOwnAuctions(int UserId)
        {
            List<ItemCategoryModel> items = new List<ItemCategoryModel>();

            foreach (var i in dbc.auctions.Where(p => p.userId == UserId).AsEnumerable().ToList())
            {
                items.Add(new ItemCategoryModel()
                {
                    Id = Convert.ToInt32(i.auctionId),
                    Title = Convert.ToString(i.title),
                    EndDate = Convert.ToDateTime(i.endDate),
                    ImageURL = Convert.ToString(i.mainImage),
                    ActualPrice = Convert.ToDouble(i.currentHighestPrice),
                    CategoryId = Convert.ToInt32(i.categoryId),
                    Description = Convert.ToString(i.description),
                    UserId = Convert.ToInt32(i.userId)
                });
            }

            return items;
        }

        public List<ItemCategoryModel> GetItemsForCategory(int categoryId)
        {   
            List<ItemCategoryModel> items = new List<ItemCategoryModel>();

            foreach (var i in dbc.auctions.Where(p => p.categoryId == categoryId).AsEnumerable().ToList())
            {
                items.Add(new ItemCategoryModel()
                {
                    Id = Convert.ToInt32(i.auctionId),
                    Title = Convert.ToString(i.title),
                    EndDate = Convert.ToDateTime(i.endDate),
                    ImageURL = Convert.ToString(i.mainImage),
                    ActualPrice = Convert.ToDouble(i.currentHighestPrice),
                    CategoryId = Convert.ToInt32(i.categoryId),
                    Description = Convert.ToString(i.description),
                    UserId = Convert.ToInt32(i.userId)
                });
            }

            return items;
        }

        public List<ItemCategoryModel> GetAllItems()
        {
            List<ItemCategoryModel> items = new List<ItemCategoryModel>();

            foreach (var i in dbc.auctions.AsEnumerable().ToList())
            {
                items.Add(new ItemCategoryModel()
                {
                    Id = Convert.ToInt32(i.auctionId),
                    Title = Convert.ToString(i.title),
                    EndDate = Convert.ToDateTime(i.endDate),
                    ActualPrice = Convert.ToDouble(i.currentHighestPrice),
                    CategoryId = Convert.ToInt32(i.categoryId),
                    Description = Convert.ToString(i.description),
                    UserId = Convert.ToInt32(i.userId)
                });
            }

            return items;
        }

        public LoginData ValidateUser(string userName, string password)
        {
            var success = dbc.users.Where(u => (u.login == userName && u.passwd == password)).ToList();

            if (success.Count() == 0)
                return null;

            user us = success[0];
            return new LoginData(Convert.ToInt32(us.userId), us.name, us.surname);
        }

        public void CreateAuction(NewAuctionModel model)
        {
            var auctions = dbc.CreateObjectSet<auction>();
            int maxId = Convert.ToInt32(auctions.Max(p => p.auctionId));
            auctions.AddObject(new auction()
            {
                auctionId = maxId + 1,
                userId = model.UserId,
                categoryId = model.CategoryId,
                title = model.Title,
                description = model.Description,
                mainImage = model.imageUrl,
                createDate = DateTime.Now,
                endDate = model.EndDate,
                startingPrice = Convert.ToDecimal(model.StartingPrice),
                currentHighestPrice = Convert.ToDecimal(model.StartingPrice),
            });
            dbc.SaveChanges();
        }

        public void DeleteAuction(int id)
        {
            try
            {
                var auctions = dbc.CreateObjectSet<auction>();
                var auctionToDelete = auctions.Where(p => p.auctionId == id).FirstOrDefault();
                auctions.DeleteObject(auctionToDelete);
                dbc.SaveChanges();
            }
            catch (Exception /*exc*/)
            {
            }
        }

        public ItemCategoryModel GetAuction(int id)
        {
            var auction = dbc.auctions.Where(p => p.auctionId == id).FirstOrDefault();

            if (auction == null || auction.auctionId != id)
                return null;

            return new ItemCategoryModel()
            {
                Id = id,
                CategoryId = Convert.ToInt32(auction.categoryId),
                Description = auction.description,
                EndDate = Convert.ToDateTime(auction.endDate),
                ActualPrice = Convert.ToDouble(auction.currentHighestPrice),
                ImageURL = auction.mainImage,
                Title = auction.title,
                UserId = Convert.ToInt32(auction.userId)
            };
        }

        public void Dispose()
        {
        }
    }
}
